using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Awaiter;
using YesPlayLove.Classes;
using jQueryApi;
using Nsb.Classes;
using Yoshi.Classes;

namespace YesPlayLove.Clusters
{
    [IgnoreNamespace]
    public class FeedBubble : Actor
    {
        private List<FeedItem> _feedList;

        public FeedBubble(Await awp, jQueryObject appendTo)
        {
            Debug.Assert(appendTo != null, "appendTo != null");
            AlivenessFeed.CurrentFeed = this;
            new Await()
                .AddAw(Master.Profile.GetFeedAw)
                .HandleDl(Exceptions.BlobLoadError, Await.IgnoreAw)
                .AddDx(
                    delegate
                    {
                        if (appendTo != null)
                            appendTo.Empty();
                        Element = jQuery.FromHtml("<div class='FeedCluster'/>")
                            ;

                        _feedList = Master.Profile.FeedList;

                        _feedList.Reverse();
                        foreach (FeedItem i in Master.Profile.FeedList)
                        {
                            AlivenessBubble.Nue(Element, i);
                        }
                        _feedList.Reverse();

                        Element.AppendTo(appendTo);
                    })
                .Commit(awp);
        }

        public void Refresh()
        {
            NewAw(Await.AsyncAw, Element.Parent());
        }

        public static void NewAw(Await awp, jQueryObject appendTo)
        {
            new FeedBubble(awp, appendTo);
        }
    }
}